import React from "react";
import PropTypes from "prop-types";
import {StyleSheet, Text, TouchableOpacity, View} from "react-native";
import chjStyle from "../../../CustomStyle";
import Util from "../../Util";

export default class CarListItem extends React.Component {
    static propTypes = {
        push: PropTypes.func,
        showPush: PropTypes.bool,
        accredit: PropTypes.bool,
    };

    static defaultProps = {
        showPush: false,
        accredit: true,
        push: () => {
        },
    };

    constructor(props) {
        super(props);
        this.state = {};
    }

    render() {
        const {showPush, accredit} = this.props;
        const carData = this.props.object;
        let ciasStatusString = Util.checkCiasOK(carData);
        let qaStatusString = Util.qaOK(carData);
        const qaStatusNormal = qaStatusString === Util.OK;
        let accred = '0';
        if (ciasStatusString === Util.OK) {
            accred = '1';
        }
        const qaInfoCheckParam = Util.checkQainfo(carData);
        if (qaStatusNormal) {
            if (qaInfoCheckParam === Util.OK) {
                accred = '2';
            }
            qaStatusString = '满足质保条件';
            qaStatusString += qaInfoCheckParam === Util.OK ? '-录入正常' : qaInfoCheckParam;
        }

        carData.basicinfo.isaccred = {name: '是否满足cias认证', value: accred}; //isaccred="0";//是否认证 0.未认证   1.已认证 2质保

        const statusString = Util.checkCarData(carData);
        const statusNormal = statusString === Util.OK;

        const cb = carData.basicinfo.carbrand.value;
        const cs = carData.basicinfo.carseries.value;
        const nameString = (cs.indexOf(cb) !== -1) ? cs : (cb + ' ' + cs);
        return (
            <TouchableOpacity
                style={[chjStyle.carListItem]}
                onPress={() => {
                    this.props.onClickedListener();
                }}>
                <Text
                    numberOfLines={1}
                    style={{
                        position: 'absolute',
                        color: '#303030',
                        textAlign: 'left',
                        backgroundColor: 'rgba(0, 0, 0, 0)',
                        fontSize: chjStyle.getActualFontSize(40),
                        top: chjStyle.getActualSizeInVertical(20),
                        left: chjStyle.getActualSizeInHorizontal(40),
                        width: chjStyle.getActualSizeInHorizontal(470),
                    }}>{nameString}</Text>


                <Text style={{
                    position: 'absolute',
                    color: '#999',
                    textAlign: 'left',
                    backgroundColor: 'rgba(0, 0, 0, 0)',
                    fontSize: chjStyle.getActualFontSize(30),
                    top: chjStyle.getActualSizeInVertical(70),
                    left: chjStyle.getActualSizeInHorizontal(40),
                    width: chjStyle.getActualSizeInHorizontal(470),
                }}>{this.props.object.basicinfo.carmodel.value.name || this.props.object.basicinfo.carmodel.value}</Text>

                <View style={{
                    position: 'absolute',
                    flex: 1,
                    top: chjStyle.getActualSizeInVertical(110),
                    left: chjStyle.getActualSizeInHorizontal(40),
                    width: chjStyle.getActualSizeInHorizontal(470),
                }}>
                    {showPush ? <Text style={{
                        color: '#f91',
                        textAlign: 'left',
                        backgroundColor: 'rgba(0, 0, 0, 0)',
                        fontSize: chjStyle.getActualFontSize(22),
                    }}>[编号]:{carData.carid ? carData.carid : ''}</Text> : null}
                    {accredit ? <Text style={{
                        color: qaStatusNormal ? '#1a1' : '#f91',
                        textAlign: 'left',
                        backgroundColor: 'rgba(0, 0, 0, 0)',
                        fontSize: chjStyle.getActualFontSize(22),
                    }}>[质保]:{qaStatusString}</Text> : null}

                </View>

                <Text style={{
                    position: 'absolute',
                    color: statusNormal ? '#1a1' : '#a11',
                    textAlign: 'left',
                    backgroundColor: 'rgba(0, 0, 0, 0)',
                    fontSize: chjStyle.getActualFontSize(22),
                    top: chjStyle.getActualSizeInVertical(178),
                    left: chjStyle.getActualSizeInHorizontal(40),
                    width: chjStyle.getActualSizeInHorizontal(470),
                }}>[录入]:{statusString}</Text>

                <Text style={{
                    position: 'absolute',
                    color: '#ccc',
                    textAlign: 'left',
                    backgroundColor: 'rgba(0, 0, 0, 0)',
                    fontSize: chjStyle.getActualFontSize(20),
                    top: chjStyle.getActualSizeInVertical(248),
                    left: chjStyle.getActualSizeInHorizontal(40),
                    width: chjStyle.getActualSizeInHorizontal(470),
                }}>[最后修改时间]:{this.props.object.lastModify}</Text>

                <View style={{
                    flex: 1,
                    position: 'absolute',
                    right: chjStyle.getActualSizeInHorizontal(40),
                    width: chjStyle.getActualSizeInHorizontal(160),
                    height: chjStyle.getActualSizeInVertical(292),
                    justifyContent: 'space-around',
                }}>
                    <TouchableOpacity
                        onPress={() => this.onUploadPress(statusNormal)}
                        style={[styles.btContainer, {
                            backgroundColor: statusNormal ? '#f91' : '#aaa',
                        }]}><Text
                        style={[styles.btText]}>{!this.props.object.carid || this.props.object.carid === '0' ? '上传' : '更新'}</Text></TouchableOpacity>

                    {accredit ? <TouchableOpacity
                        onPress={() => {
                            this.onQAPress(qaStatusNormal);
                        } }
                        style={[styles.btContainer, {
                            backgroundColor: qaStatusNormal ? '#1a1' : '#aaa',
                        }]}><Text style={[styles.btText]}>质保</Text></TouchableOpacity> : null}

                    {showPush ? <TouchableOpacity
                        onPress={() => {
                            if (carData.carid) {
                                this.props.push();
                            }
                        } }
                        style={[styles.btContainer, {
                            backgroundColor: carData.carid ? '#1a1' : '#aaa',
                        }]}><Text style={[styles.btText]}>推送</Text></TouchableOpacity> : null}

                    <TouchableOpacity
                        onPress={this.props.onClickedDeleteListener}
                        style={[styles.btContainer, {
                            backgroundColor: '#a11',
                        }]}><Text style={[styles.btText]}>删除</Text></TouchableOpacity>
                </View>

            </TouchableOpacity>
        );
    }

    onUploadPress(enable) {
        if (enable) {
            this.props.onClickedUploadListener();
        }
    }

    onQAPress(enable) {
        if (enable) {
            this.props.onClickedQAListener();
        }
    }
}

const styles = StyleSheet.create({
    btContainer: {
        borderRadius: 10,
        borderTopWidth: 1,
        borderLeftWidth: 1,
        borderRightWidth: 1,
        borderBottomWidth: 1,
        borderColor: '#fff',
        width: chjStyle.getActualSizeInHorizontal(160),
        height: chjStyle.getActualSizeInVertical(62),
        justifyContent: 'center',
        alignItems: 'center',
    },
    btText: {
        backgroundColor: 'rgba(0,0,0,0)',
        color: '#fff',
        textAlign: 'center',
        fontSize: chjStyle.getActualFontSize(30)
    },
});

